
package com.netease.xone.widget.richtext.span;

import media.MediaPlayerWrapper;
import media.MediaPlayerWrapper.MediaListener;
import android.content.Context;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;

import com.netease.xone.R;
import com.netease.xone.widget.richtext.IRichTextClickListener;

public class ImageAudioSpan extends ClickableSpan implements MediaListener{

    public static final String REGULAR_IMAGE_AUDIO = "\\[音(\\S+?)\\]";
    
    private String mValue;
    private String mUrl;
    private IRichTextClickListener mListener;
    
    public ImageAudioSpan() {
        
    }
    
    public ImageAudioSpan(String value, IRichTextClickListener listener) {
        this.mValue = value;
        this.mUrl = mValue.substring("[音:".length(), mValue.length()-1);
        this.mListener = listener;
        MediaPlayerWrapper.getInstance().registerMediaListener(this);
    }
    
    public void setSpan(Context context, Spannable sp, int start, int end) {
        mContext = context;
        mSpannable = sp;
        mStart = start;
        mEnd = end;
        if (isMyself()) {
            sp.setSpan(new ImageSpan(context, R.drawable.icon_audio_pause), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            sp.setSpan(new ImageSpan(context, R.drawable.icon_audio_play), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        
        // ImageSpan加click事件
        sp.setSpan(this, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    }

    private Context mContext;
    private Spannable mSpannable;
    private int mStart;
    private int mEnd;
    
    @Override
    public void onClick(View view) {
//        Toast.makeText(view.getContext(), "图片和音频", Toast.LENGTH_SHORT).show();
        if (null != mListener) {
            mListener.onRichTextClick(view, mValue, IRichTextClickListener.RICHTEXT_IMAGE_AUDIO);
        }
        MediaPlayerWrapper mediaPlayer = MediaPlayerWrapper.getInstance();
        if (view.isSelected()) {
            view.setSelected(false);
            mediaPlayer.pause();
        } else {
            view.setSelected(true);
            if (!isMyself()) {
                mediaPlayer.play(mUrl);
                mediaPlayer.setName(mUrl);
            } else {
                mediaPlayer.play();
            }
        }
    }
   
    private boolean isMyself() {
        boolean bRes = false;
        if (mUrl.equals(MediaPlayerWrapper.getInstance().getFilePath())) {
            bRes = true;
        }
        return bRes;
    }

    @Override
    public void onMediaPlay() {
        if (isMyself()) {
            mSpannable.setSpan(new ImageSpan(mContext, R.drawable.icon_audio_pause), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    @Override
    public void onMediaPause() {
        if (isMyself()) {
            mSpannable.setSpan(new ImageSpan(mContext, R.drawable.icon_audio_play), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } 
    }

    @Override
    public void onMediaRelease() {
        MediaPlayerWrapper.getInstance().registerMediaListener(this);
        mSpannable.setSpan(new ImageSpan(mContext, R.drawable.icon_audio_play), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    @Override
    public void onMediaCompletion() {
        MediaPlayerWrapper.getInstance().release();
    }
}
